package dao;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import model.User;
import uitl.DatabaseConnection;


public class UserDaoImpl implements UserDAO{
	private PreparedStatement stat = null;
	DatabaseConnection dbc;
	public UserDaoImpl(){
		 dbc=new DatabaseConnection();
	}
	//===================================创建用户
	public boolean doCreate(User user)throws Exception{
		
		stat = dbc.getConnection().prepareStatement("INSERT INTO user VALUES(?,?,?)");
		stat.setInt(1,user.getUserid());
		stat.setString(2,user.getPassword());
		stat.setString(3,user.getUsername());
		
		int update = stat.executeUpdate();
		if(update>0){
			stat.close();
			dbc.getConnection().close();
			return true;
			
		}
		else{
			stat.close();
			dbc.getConnection().close();
			return false;
			
		}
	}
	//=========================依据信息查找用户
	public List<User> findAll()throws Exception{
		String sql = "SELECT * FROM user";
		stat = dbc.getConnection().prepareStatement(sql);
		ResultSet rs = stat.executeQuery();
		User user = null;
		 List<User> list=new ArrayList<User>();
		while(rs.next()){
			int userid = rs.getInt(1);
			String password = rs.getString(2);
			String username = rs.getString(3);
		
			user = new User();
			user.setUserid(userid);
			user.setPassword(password);
			user.setUsername(username);
		
		}
		rs.close();
		stat.close();
		dbc.getConnection().close();
		return list;
	}
	//=========================================实现更新操作
	public boolean doupdate(User user)throws Exception{
		String sql;
		String password = user.getPassword();
		int userID = user.getUserid();
		int judge = 0;
		try {
			sql = "update users set password = '" + password + "' where cardid = '"
					+ userID + "'";
			PreparedStatement rs = dbc.getConnection().prepareStatement(sql);
			judge = rs.executeUpdate();
		} catch (Exception e1) {
			e1.printStackTrace();
		}
		if(judge>0)
			return true;
		else
			return false;
	}
	
	//======================================删除操作
	public boolean dodelete(String userid)throws Exception{
		String sql;
		int judge = 0;
		try {
			sql = "delete from users where cardid = '" + userid + "'";
			PreparedStatement stmt = dbc.getConnection().prepareStatement(sql);
			stmt.executeQuery(sql);
			judge=stmt.executeUpdate();
		} catch (Exception e1) {
			e1.printStackTrace();
		}
		if(judge>0)
			return true;
		else
			return false;
	}
	
	//======================================根据用户账户查找信息
	public User findById(int userid)throws Exception{
		String sql = "SELECT password,username FROM user WHERE userid=?";
		stat = dbc.getConnection().prepareStatement(sql);
		stat.setInt(1,userid);
		ResultSet rs = stat.executeQuery();
		User user = null;
		if(rs.next()){
			String password = rs.getString(2);
			String username = rs.getString(3);
			
			user = new User();
			user.setUserid(userid);
			user.setPassword(password);
		    user.setUsername(username);
			
		}
		rs.close();
		stat.close();
		dbc.getConnection().close();
		return user;
	}

}